<template>
  <div class="form-wrapper">
    <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" size="large">
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" placeholder="手机号\账号">
          <template #prefix>
            <el-icon class="el-input__icon"><user /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="loginForm.password" placeholder="密码" show-password autocomplete="new-password">
          <template #prefix>
            <el-icon class="el-input__icon"><lock /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <div class="captcha-frame-wrapper">
        <div class="captcha-frame">
          <el-form-item prop="verifyCode">
            <el-input placeholder="验证码" v-model="loginForm.verifyCode" @keyup.enter="login">
              <template #prefix>
                <el-icon class="el-input__icon"><Key /></el-icon>
              </template>
            </el-input>
          </el-form-item>
        </div>
        <div class="captcha-box" @click="getCaptcha">
          <img :src="Captcha_Base64_url" alt="" />
        </div>
      </div>
    </el-form>
    <div class="login-btn-wrapper">
      <el-button :icon="CircleClose" round @click="resetForm(loginFormRef)" size="large">重置</el-button>
      <el-button :icon="UserFilled" @click="login(loginFormRef)" size="large" type="primary" :loading="loading" class="login-btn">
        登录
      </el-button>
    </div>
    <div class="info-wrapper">
      <el-checkbox v-model="checked2"> </el-checkbox>
      <span class="note note-ml10">我已阅读并同意千万职联</span>
      <el-button type="primary" link class="info-btn">用户协议</el-button> <span class="note">和</span>
      <el-button type="primary" link class="info-btn">隐私条款</el-button>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ElForm } from "element-plus";
import { onMounted, reactive, ref } from "vue";
import { CircleClose, UserFilled } from "@element-plus/icons-vue";
import md5 from "md5";
import { bizCaptcha, bizLogin, bizOptToken } from "@/biz/modules/user";

const loading = ref(false);
const checked2 = ref(true);
// 登录表单数据
const loginForm = reactive({
  // username: "18766295274",
  // password: "123456",
  username: "",
  password: "",
  optCode: "",
  verifyCode: "",
  clientId: "33f25d5380d6a9ff4c2abc782ca3c911",
  clientPassword: "C19152147AB60D08653B34864A1A7D11"
});
// 定义 formRef（校验规则）
type FormInstance = InstanceType<typeof ElForm>;
const loginFormRef = ref<FormInstance>();
const loginRules = reactive({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  verifyCode: [{ required: true, message: "请输入验证码", trigger: "blur" }]
});

const Captcha_Base64_url = ref("");
// login
const login = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate(async valid => {
    if (!valid) return;
    loading.value = true;
    try {
      bizLogin({ ...loginForm, password: md5(loginForm.password) });
    } finally {
      loading.value = false;
    }
  });
};

// 获取验证码
const getCaptcha = () => {
  bizOptToken()
    .then(res => {
      if (res.data) {
        let optCode = res.data as unknown as string;
        loginForm.optCode = optCode;
        bizCaptcha(optCode)
          .then((res: any) => {
            Captcha_Base64_url.value = "data:image/gif;base64," + res.data;
          })
          .catch(err => {
            console.log("", err);
          });
      }
    })
    .catch(err => {
      console.log("", err);
    });
};

// resetForm
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

onMounted(() => {
  //获取验证码
  getCaptcha();

  // 监听enter事件（调用登录）
  document.onkeydown = (e: any) => {
    e = window.event || e;
    if (e.code === "Enter" || e.code === "enter" || e.code === "NumpadEnter") {
      if (loading.value) return;
      login(loginFormRef.value);
    }
  };
});
</script>
<style scoped lang="scss">
.form-wrapper {
  position: relative;
  height: 320px;
  width: 100%;
}
.el-form-item {
  margin-bottom: 20px;
}
.login-btn-wrapper {
  display: flex;
  justify-content: space-between;
  width: 100%;
  margin-top: 20px;
  white-space: nowrap;
  .el-button {
    width: 100%;
  }
}

.captcha-frame-wrapper {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;

  .captcha-frame {
    width: 48%;
  }
  .captcha-box {
    width: 48%;
    height: 2.5rem;
    cursor: pointer;
    & img {
      width: 100%;
      height: 100%;
    }
  }
}

.info-wrapper {
  position: absolute;
  width: 100%;
  bottom: 20px;

  display: flex;
  align-items: center;
  justify-content: center;

  .note {
    color: var(--el-color-info);
    font-size: 12px;
    font-weight: 600;
  }

  .note-ml10 {
    margin-left: 10px;
  }
}
</style>
